package com.app.teaware.stock;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.teaware.R;
import com.app.teaware.basis.BasisActivity;
import com.app.teaware.basis.Constants;
import com.app.teaware.bean.BasisBean;
import com.app.teaware.bean.LoginBean;
import com.app.teaware.bean.StockListBean;
import com.app.teaware.bean.StockListData;
import com.app.teaware.dialog.ResetKWDialog;
import com.app.teaware.util.MyUtils;
import com.app.teaware.util.UHFUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.MathUtils;
import my.http.MyHttpListener;
import my.http.MyHttpUtils;
import my.http.MyParams;

public class StockInfoActivity extends BasisActivity {


    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    StockListBean listBean;
    MyAdapterList mAdapter;
    String wms;

    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.base_recycler_with_top);
        setTitle("货品资料");
        setTitleLeftButton(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
    }


    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        listBean = new StockListBean();
        wms = getIntent().getStringExtra("wms");
        getList();
    }


    private void getList(){
        showLoading();
        MyParams params = new MyParams();
        params.put("aid",LoginBean.getInstance().getAcode());
        params.put("wms",wms);

        MyHttpUtils.get(Constants.URL_STOCK_GET_CHECK_STOCK_BY_LOC, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        if (isFinishing()) return;
                        listBean = (StockListBean) result;
                        setListView();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
                        dismissProgress();
                        listBean.getList().clear();
                        setListView();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        dismissProgress();
                    }
                },
                0, StockListBean.class);
    }

    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
    }

    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder>
    {
        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.reset_ware_list_item, viewGroup, false);
            return new ViewHolder(view);
        }

        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            int count = listBean == null ? 0 : listBean.getList().size();
            return count;
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.text_code)
            public TextView text_code;
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.text_specs)
            public TextView text_specs;
            @BindView(R.id.text_unit)
            public TextView text_unit;
            @BindView(R.id.text_num)
            public TextView text_num;
            @BindView(R.id.text_weight)
            public TextView text_weight;
            @BindView(R.id.text_kw)
            public TextView text_kw;
            @BindView(R.id.text_qrcode)
            public TextView text_qrcode;

            @BindView(R.id.text_weight_fact)
            public TextView text_weight_fact;
            @BindView(R.id.btn_edit)
            public TextView btn_edit;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                StockListData data = listBean.getList().get(position);
                MyUtils.setNotNullText(text_code, data.getGno());
                MyUtils.setNotNullText(text_name, data.getGna());
                MyUtils.setNotNullText(text_specs, data.getGsp());
                MyUtils.setNotNullText(text_unit, data.getGun());
                MyUtils.setNotNullText(text_num, data.getGnu1());
                MyUtils.setNotNullText(text_weight, data.getGwtp());
                MyUtils.setNotNullText(text_kw, data.getWms1());
                MyUtils.setNotNullText(text_qrcode, data.getRfid());
                MyUtils.setNotNullText(text_weight_fact, MathUtils.div(MathUtils.multiply(data.getGwtp(),data.getGnu1()),"1000"));
                if(data.isSelect()){
                    list_item.setBackgroundResource(R.drawable.base_blue_light_roundcorner);
                }else {
                    list_item.setBackgroundResource(R.drawable.base_white_round);
                }
                btn_edit.setTag(data);
                list_item.setTag(data);
            }

            @OnClick(R.id.list_item)
            void onItemClick(View view){
//                StockListData data = (StockListData) view.getTag();
//                data.setSelect(!data.isSelect());
//                notifyItemChanged(getAdapterPosition());
            }
        }
    }

}
